// pages/coupon/coupon.js
const api = require("../../utils/interface.js")
const math = require("../../utils/math.js")
const tools = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    couponList: [],
    routerData: null,
    superposition: [],
    currentIndex: 0,
    imagestate: 0,//判断是否可叠加的状态
    selectCoupon:[],
    orderAmount:0
  },
  onLoad: function (options) {
    let that = this
    console.log(options)
   
    let data = JSON.parse(options.couponList)
    this.setData({
      orderAmount: math.divide(data.orderAmount,100)
    })
    this.getCouponList(data)
  },
  goback() {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      getcoupnPrevent: 1
    })
    wx.navigateBack()
  },
  godetails(e) {
    console.log(e, "e")
    let couponId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/coupondetails/details?memberCouponId=' + couponId
    })
  },
  handleClick(e) {
    const index = e.currentTarget.dataset.idx
    const index1 = e.currentTarget.dataset.index
    let preAmount = 0
    let minimumAmount = 0
    // 不可叠加
    if (this.data.couponList[index].superposition==0){
      this.data.selectCoupon = []
      this.data.couponList[index].radio = !this.data.couponList[index].radio
      this.data.selectCoupon.push(this.data.couponList[index])
      this.data.couponList.map(item=>{
        if (this.data.couponList[index].id!=item.id){
          item.radio = false
        }
      })      
    }else{
      // 可以叠加
      // this.data.selectCoupon = []
      this.data.couponList[index].radio = !this.data.couponList[index].radio
      this.data.couponList.map(item=>{
        item.radio && this.data.selectCoupon.push(item)
        item.superposition == 0 && (item.radio = false)
      })
      this.data.selectCoupon = unique(this.data.selectCoupon)
      
      
      this.data.selectCoupon.map((item,idx)=>{
        if (!item.radio){
          this.data.selectCoupon.splice(idx,1)
          console.log(this.data.selectCoupon)
        }
      })
      console.log(this.data.selectCoupon)
      this.data.selectCoupon.map((item, idx) => {
        preAmount += item.preAmount
        minimumAmount += item.minimumAmount
      })
      
      console.log(minimumAmount, preAmount,"狗男")
      if (preAmount > this.data.orderAmount || minimumAmount > this.data.orderAmount){
        this.data.selectCoupon = truncate(this.data.selectCoupon)
        this.data.couponList[index].radio = false
      }
    }
    // 可以叠加 orderAmount
    this.setData({
      currentIndex: index,
      couponList: this.data.couponList
    })
    function truncate(arr) {
      return arr.filter(function (ele, idx, arr) {
        console.log(idx)
        return arr.length - 1 !== idx;
      });
    }
    
    function unique(arr) {
      for (var i = 0; i < arr.length; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].id == arr[j].id) {         //第一个等同于第二个，splice方法删除第二个
            arr.splice(j, 1);
            j--;
          }
        }
      }
      return arr;
    }
  },
  // 获取优惠券列表
  getCouponList(data){
   wx.showLoading({
     title: '加载中...',
   })
    api.getCoupon(data).then(res => {
      wx.hideLoading()
      if (res.data.result.length > 0) {
        let a = []
        let b = []
        res.data.result.map((item, index) => {
          
          item.radio = false
          item.preAmount = math.divide(item.preAmount, 100)
          item.minimumAmount = math.divide(item.minimumAmount, 100)
          if (item.superposition==1){
            item.state = true
            a.push(item)
          }else{
            item.state2 = true
            b.push(item)
          }
        })
        let c = tools.ArrayMax(a, "preAmount")
        let d = tools.ArrayMax(b, "preAmount")
        this.data.couponList = c.concat(b)
        this.setData({
          couponList: this.data.couponList
        })
       
      }

    })
  },
  onUnload(){
    if (this.data.selectCoupon){
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        selectCouponList: this.data.selectCoupon
      })
    }
    
  }

  
})